import { pgTable, text, serial, integer, boolean, timestamp, jsonb } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";

export const proxyRules = pgTable("proxy_rules", {
  id: serial("id").primaryKey(),
  name: text("name").notNull().unique(),
  description: text("description"),
  type: text("type").notNull(), // HTTP, HTTPS, TCP, UDP
  localPort: integer("local_port").notNull(),
  remotePort: integer("remote_port").notNull(),
  targetHost: text("target_host").notNull().default("localhost"),
  status: text("status").notNull().default("stopped"), // running, stopped, error
  loadBalancing: text("load_balancing").default("round-robin"), // round-robin, least-connections, ip-hash
  healthCheck: boolean("health_check").default(true),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow().notNull(),
});

export const backendServers = pgTable("backend_servers", {
  id: serial("id").primaryKey(),
  proxyRuleId: integer("proxy_rule_id").references(() => proxyRules.id).notNull(),
  host: text("host").notNull(),
  port: integer("port").notNull(),
  weight: integer("weight").default(1),
  status: text("status").notNull().default("healthy"), // healthy, unhealthy, disabled
  responseTime: integer("response_time").default(0),
  lastHealthCheck: timestamp("last_health_check"),
  createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const systemMetrics = pgTable("system_metrics", {
  id: serial("id").primaryKey(),
  cpuUsage: integer("cpu_usage").notNull(),
  memoryUsage: integer("memory_usage").notNull(),
  networkIO: text("network_io").notNull(),
  activeConnections: integer("active_connections").notNull(),
  totalTraffic: text("total_traffic").notNull(),
  errorRate: text("error_rate").notNull(),
  avgResponseTime: integer("avg_response_time").notNull(),
  timestamp: timestamp("timestamp").defaultNow().notNull(),
});

export const proxyLogs = pgTable("proxy_logs", {
  id: serial("id").primaryKey(),
  level: text("level").notNull(), // INFO, DEBUG, WARN, ERROR
  message: text("message").notNull(),
  proxyRuleId: integer("proxy_rule_id").references(() => proxyRules.id),
  timestamp: timestamp("timestamp").defaultNow().notNull(),
  metadata: jsonb("metadata"),
});

export const insertProxyRuleSchema = createInsertSchema(proxyRules).omit({
  id: true,
  createdAt: true,
  updatedAt: true,
});

export const insertBackendServerSchema = createInsertSchema(backendServers).omit({
  id: true,
  createdAt: true,
  lastHealthCheck: true,
});

export const insertSystemMetricsSchema = createInsertSchema(systemMetrics).omit({
  id: true,
  timestamp: true,
});

export const insertProxyLogSchema = createInsertSchema(proxyLogs).omit({
  id: true,
  timestamp: true,
});

export type ProxyRule = typeof proxyRules.$inferSelect;
export type InsertProxyRule = z.infer<typeof insertProxyRuleSchema>;
export type BackendServer = typeof backendServers.$inferSelect;
export type InsertBackendServer = z.infer<typeof insertBackendServerSchema>;
export type SystemMetrics = typeof systemMetrics.$inferSelect;
export type InsertSystemMetrics = z.infer<typeof insertSystemMetricsSchema>;
export type ProxyLog = typeof proxyLogs.$inferSelect;
export type InsertProxyLog = z.infer<typeof insertProxyLogSchema>;

export type User = {
  id: number;
  username: string;
  password: string;
};

export type InsertUser = {
  username: string;
  password: string;
};
